package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.common.exception.LogistException;
import cn.wolfcode.luowowo.flight.domain.BuyTicket;
import cn.wolfcode.luowowo.flight.domain.DoorTicket;
import cn.wolfcode.luowowo.flight.query.DoorTicketQueryObject;
import cn.wolfcode.luowowo.flight.service.IBuyTicketService;
import cn.wolfcode.luowowo.flight.service.IDoorTicketService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.anno.UserParm;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("ticket")
public class DoorTicketController {

    @Reference
    private IDoorTicketService doorTicketService;
    @Reference
    private IBuyTicketService buyTicketService;
    //门票首页
    @RequestMapping("/index")
    public String index(Model model, @ModelAttribute("qo") DoorTicketQueryObject qo){
        List<DoorTicket> tickets = doorTicketService.seletTicketList(qo);
        model.addAttribute("tickets",tickets);
        return "ticket/index";
    }
    //导航回显
    @RequestMapping("/pa")
    public String pa(Model model, @ModelAttribute("qo") DoorTicketQueryObject qo){
        List<DoorTicket> tickets = doorTicketService.seletTicketList(qo);
        model.addAttribute("tickets",tickets);
        return "ticket/indexTpl";
    }
    //查询单个门票的信息
    @RequestMapping("/detail")
    public String detail(Model model,Long tid){
        DoorTicket ticketDetail = doorTicketService.queryById(tid);
        model.addAttribute("ticketDetail",ticketDetail);
        return "ticket/detail";
    }
    //查询单个门票回显到订单表中
    @RequestMapping("/addOrder")
    public String addOrder(Model model,Long tid){
        DoorTicket ticketDetail = doorTicketService.queryById(tid);
        model.addAttribute("ticketDetail",ticketDetail);
        return "ticket/addOrder";
    }
    //门票订单
    @RequestMapping("/create")
    public String create(Model model, BuyTicket ticket, @UserParm UserInfo userInfo,@ModelAttribute("qo") DoorTicketQueryObject qo){
        if(userInfo == null){
            throw  new LogistException("请登录");
        }
        ticket.setUserinfoId(userInfo.getId());
        buyTicketService.saveOrUpdate(ticket);
        List<DoorTicket> tickets = doorTicketService.seletTicketList(qo);
        model.addAttribute("tickets",tickets);
        return "ticket/index";
    }
    //查询门票的订单
    @RequestMapping("/showDian")
    public String create(Model model,@UserParm UserInfo userInfo){
        if(userInfo == null){
            throw  new LogistException("请登录");
        }
        List<BuyTicket> doorTickets = buyTicketService.queryByUserId(userInfo.getId());
        model.addAttribute("doorTickets",doorTickets);
        return "mine/myorder";
    }
}
